package cn.david.basic.util;

import cn.david.user.domian.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {
    @Autowired
    RedisTemplate redisTemplate;

    public static Logininfo getLoginInfo(HttpServletRequest request){
        //获取当前用户信息

        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        RedisTemplate redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);
        String token = request.getHeader("token");
        Object o = redisTemplate.opsForValue().get(token);
        if (o!=null){
            return (Logininfo) o;
        }
        return null;
    }
}
